package com.hfh.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author hfh
 * @Date 2022 09 19 22 06
 **/
public class JDBCBatch {
    public static void main(String[] args) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            con = DBUtils.getConnection();
            String sql = "insert into admin values(null,?,?)";
            ps = con.prepareStatement(sql);

            /*for (int i = 1;i<10000;i++){
                ps.setString(1,"3");
                ps.setString(2,"3");
                ps.executeUpdate();
            }*/
            for (int i = 1;i<=10000;i++){
                ps.setString(1,"3");
                ps.setString(2,"3");
                ps.addBatch();
                if(i%1000==0){
                    ps.executeBatch();//执行批处理包里的语句
                    ps.clearBatch();
                }
            }



//            ps.addBatch();
//            ps.executeBatch();
//            rs = ps.executeQuery();
            while (rs.next()){
                System.out.println(rs.getString(1));
                System.out.println(rs.getString(2));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtils.close(con,ps,rs);
        }
    }
}
